// components/container/container.js
var app = getApp()

Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
  },
  options: {
  },
  externalClasses: ['container-class', 'single-btn'],

  /**
   * 组件的初始数据
   */
  data: {
  },
  /**
   * 组件的方法列表
   */
  methods: {
    navLevelInfo() {
      wx.navigateTo({
        url: '/pages/levelInfo/levelInfo',
      })
    },
    navIntegrationInfo() {
      wx.navigateTo({
        url: '/pages/integrationInfo/integrationInfo',
      })
    },
  },
  ready() {
    app.globalData.getCommonData().then(() => {
      let clientInfo = app.globalData.clientInfo
      let levelObj = app.globalData.levelObj
      let canUsePoints = app.globalData.clientInfo.canUsePoints
      let levelPoints = clientInfo.levelPoints
      let levelIndex = clientInfo.levelIndex
      let levelName = clientInfo.levelName
      this.setData({
        clientInfo,
        levelObj
      })

      //进度条这边的逻辑
      if (+levelIndex === -1) {
        this.setData({
          progressLevelInfo: `${levelObj.levelDetail[0].levelName}（${levelObj.levelDetail[0].upgradeThreshold}）`,
          progressPer: '0%'
        })
      }
      else if (+levelPoints > +canUsePoints) {
        this.setData({
          progressLevelInfo: `${levelName}（0）`,
          progressPer: '100%'
        })
      }
      else {
        let current = levelObj.levelDetail[levelIndex]
        let next = levelObj.levelDetail[levelIndex + 1]
        let progressPer = (levelPoints - current.upgradeThreshold) / (canUsePoints - current.upgradeThreshold) * 100 + '%'
        this.setData({
          progressLevelInfo: `${next.name}（${next.upgradeThreshold - levelPoints}）`,
          progressPer
        })
      }
      // let clientInfo = app.globalData.clientInfo
      // let levelObj = app.globalData.levelObj
      // this.setData({
      //   clientInfo,
      //   levelObj
      // })

      // let filterIndex = levelObj.levelDetail.findIndex(val => {
      //   return val.level == clientInfo.level
      // })

      // if (filterIndex !== levelObj.levelDetail.length - 1) {
      //   this.setData({
      //     progressLevelInfo: `${levelObj.levelDetail[filterIndex].name}（${levelObj.levelDetail[filterIndex].upgradeThreshold}）`,
      //     progressPer: ''
      //   })
      // }else{
      //   this.setData({
      //     progressLevelInfo: `（${levelObj.levelDetail[filterIndex].upgradeThreshold}）`,
      //     progressPer: '100%'
      //   })
      // }



      // let point = app.globalData.clientInfo.levelPoints
      // function judge(arr, num) {
      //   var i = 0, sum = 0, len = arr.length;
      //   if (!(len > 0)) {
      //     return;
      //   }
      //   //如果 num 的值超过了 数组之和，不在考虑范围之类
      //   for (i; i < len; i++) {
      //     sum += +arr[i];
      //     console.log(+arr[i], +num)
      //     if (sum > +num) {
      //       return i - 1;
      //     }
      //   }
      //   // if(+num > sum)
      // }
      // let activeIndex = judge(levelDetail.map(val => val.upgradeThreshold), point)
      // console.log(levelDetail.map(val => val.upgradeThreshold), point, activeIndex)
      // this.setData({
      //   activeIndex
      // })
    })
  }
})
